﻿using System.IO;
using System.Linq;
using KTOP.Core.Domain.Matrix;

namespace KTOP.Core.Formatters
{
    public class MatrixFormatter
    {
        public void Format(MatrixWithSumms matrix, string name, TextWriter tw)
        {
            var matrixTable = new[] { new[] { "" }.Concat(Enumerable.Range(1, matrix.Size).Select(t => t.ToString())).ToArray() }
                .Concat(

                matrix.Matrix
                .Zip(matrix.Summs, (row, summ) => row.Concat(new[] { summ }).Select(t => t.ToString()))
                .Zip(Enumerable.Range(1, matrix.Size), (mstr, rowNum) => new[] { rowNum.ToString()}.Concat(mstr))
                .Select(r => r.ToArray())
             
            )
            .ToArray();



            tw.WriteLine(name);
            tw.WriteLine();

            var formatter = new TextTableFormatter();
            formatter.Format(tw, matrixTable, TextTableFormatter.Borders.None);
        }
    }
}
